package com.duobaoyu.dby.supply.biz.client.boundary.channel;

import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.duobaoyu.dby.channel.adapter.bean.refund.AfterSaleConfig;
import com.duobaoyu.dby.channel.adapter.bean.refund.AfterSaleReason;
import com.duobaoyu.dby.channel.adapter.bean.refund.AfterSaleReturnAddressConfig;
import com.duobaoyu.dby.channel.adapter.bean.refund.ExpressInfo;
import com.duobaoyu.dby.channel.adapter.service.AbstractSupplyAfterSaleService;
import com.duobaoyu.dby.common.core.constant.CommonConstants;
import com.duobaoyu.dby.supply.biz.client.resource.ResourceApiClient;
import com.duobaoyu.dby.supply.biz.client.resource.bean.bo.AfterSaleConfigBO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.bo.AfterSaleReasonBO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.bo.AfterSaleSettingConfigBO;
import com.duobaoyu.dby.supply.biz.client.resource.bean.bo.ExpressInfoBO;
import com.duobaoyu.dby.supply.db.entity.refund.AfterSale;
import com.duobaoyu.dby.supply.db.entity.refund.CustomerReturnAddress;
import com.duobaoyu.dby.supply.db.repository.refund.AfterSaleRepository;
import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Component;

import java.util.*;
import java.util.stream.Collectors;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName SupplyChainAfterSaleConfigService
 * @Description 渠道售后配置
 * @createTime 18:28:00 2023/10/16
 */
@Component
@RequiredArgsConstructor
public class SupplyChainAfterSaleConfigService extends AbstractSupplyAfterSaleService {

    private final ResourceApiClient resourceApiClient;
    private final AfterSaleRepository afterSaleRepository;

    @Override
    public List<AfterSaleConfig> channelAfterSaleConfigs(String channelCode) {
        AfterSaleSettingConfigBO afterSaleSettingConfig = resourceApiClient.queryAfterSaleSettingConfig(channelCode);
        List<AfterSaleConfigBO> afterSaleConfigs = Optional.ofNullable(afterSaleSettingConfig).map(AfterSaleSettingConfigBO::getAfterSaleConfigs).orElse(Lists.newArrayList());
        return afterSaleConfigs.stream().map(afterSaleConfig -> AfterSaleConfig
                        .builder()
                        .configName(afterSaleConfig.getConfigName())
                        .repositoryTypes(afterSaleConfig.getRepositoryTypes())
                        .afterSaleTypes(afterSaleConfig.getAfterSaleTypes())
                        .afterSaleRules(CollectionUtils.isEmpty(afterSaleConfig.getAfterSaleRules()) ?
                                Collections.emptyList() :
                                afterSaleConfig.getAfterSaleRules().stream().map(rule -> AfterSaleConfig.AfterSaleRule
                                                .builder()
                                                .afterSaleType(rule.getAfterSaleType())
                                                .orderStatus(rule.getOrderStatus())
                                                .build())
                                        .collect(Collectors.toList()))
                        .explainUrl(afterSaleConfig.getExplainUrl())
                        .build())
                .collect(Collectors.toList());
    }


    @Override
    public Map<Integer, List<AfterSaleReason>> afterSaleReasons(List<Integer> afterSaleTypes) {
        Map<Integer, List<AfterSaleReasonBO>> reasonMap = resourceApiClient.queryAfterSaleReason(afterSaleTypes);
        Map<Integer, List<AfterSaleReason>> resultMap = Maps.newHashMap();
        reasonMap.forEach((k, v) -> resultMap.put(k, v.stream().filter(reason -> Objects.equals(reason.getStatus(), CommonConstants.ONE)).map(reason -> AfterSaleReason.builder().afterSaleType(reason.getAfterSaleType()).reasonCode(reason.getReasonCode()).reasonName(reason.getReasonName()).sort(reason.getSort()).build()).collect(Collectors.toList())));
        return resultMap;
    }

    @Override
    public Map<String, ExpressInfo> expressInfo(String channelCode) {
        Map<String, ExpressInfoBO> expressInfoMap = resourceApiClient.getExpressInfoAllCacheByChannelCode(channelCode);
        Map<String, ExpressInfo> resultMap = Maps.newHashMap();
        expressInfoMap.forEach((k, v) -> resultMap.put(k, ExpressInfo.builder().expressCompanyCode(v.getExpressCompanyCode()).expressCompanyName(v.getExpressCompanyName()).channelCode(v.getChannelCode()).channelExpressCode(v.getChannelExpressCode()).sort(v.getSort()).build()));
        return resultMap;
    }

    @Override
    public AfterSaleReturnAddressConfig returnAddressConfig(String afterSaleSn) {
        AfterSale afterSale = afterSaleRepository.getOne(Wrappers.<AfterSale>lambdaQuery().eq(AfterSale::getAfterSaleSn, afterSaleSn));
        CustomerReturnAddress customerReturnAddress = Optional.ofNullable(afterSale).map(AfterSale::getCustomerReturnAddress).orElse(new CustomerReturnAddress());
        return AfterSaleReturnAddressConfig.builder()
                .afterSaleSn(Optional.ofNullable(afterSale).map(AfterSale::getAfterSaleSn).orElse(StringPool.EMPTY))
                .afterSaleTradeSn(Optional.ofNullable(afterSale).map(AfterSale::getAfterSaleTradeOrderSn).orElse(StringPool.EMPTY))
                .afterServiceReceiver(customerReturnAddress.getChannelAfterServiceReceiver())
                .afterServiceAddress(customerReturnAddress.getChannelAfterServiceAddress())
                .afterServicePhone(customerReturnAddress.getChannelAfterServicePhone())
                .afterServiceTel(customerReturnAddress.getChannelAfterServiceTel())
                .build();
    }
}
